﻿//*** Guidance generated code ***//
using System;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using System.Linq;
using System.Xml.Linq;

using ProjectBase.Core;
using ProjectBase.Data;
using ProjectBase.Data.Xml;

using Survey.Common;
using Survey.Common.Data.Services;
using Survey.Entity.Services;

namespace Survey.Data.Services.Xml
{
    /// <summary>
    ///     Provides GetAll() which returns all languages in the application
    ///     and extends Find
    /// </summary>
    public class LanguageDao : BaseXmlDao<Language, int, LanguageFilter>, ILanguageDao
    {
        #region Find
        /// <summary>
        ///     Fills the Criteria list with where statements based on provided filter
        /// </summary>
        /// <param name = "filter"></param>
        /// <returns></returns>
        public override IEnumerable<Language> Find(LanguageFilter filter)
        {
            // Equal
            Criteria.Add(entity => entity.ID.IsEqual(filter.IdExact));
            Criteria.Add(entity => entity.LanguageName.IsExact(filter.LanguageNameSearch));

            // Like
            Criteria.Add(entity => entity.LanguageName.IsLike(filter.LanguageName));

            return GetListByCriteria(filter);
        }
        #endregion Find

        #region ILanguageDao Members
        /// <summary>
        ///     Returns all available Languages (e.g. for UI as the option list)
        /// </summary>
        /// <returns></returns>
        public virtual IEnumerable<Language> GetAll()
        {
            return TheList;
        }
        #endregion

        #region XML - ENTITY mapping
        /// <summary>
        ///     Converts the XElement into the Language
        /// </summary>
        /// <param name = "element"></param>
        /// <returns></returns>
        protected override Language CreateEntity(XElement element)
        {
            var entity = FillEntity(new Language(), element); // base converts ValueTypes and String
            entity.SetId((int)element.Attribute(Str.Common.ID));
            return entity;
        }

        /// <summary>
        ///     Converts the Language instnace into the XElement
        /// </summary>
        /// <param name = "entity"></param>
        /// <returns></returns>
        protected override XElement CreateElement(Language entity)
        {
            var element = base.CreateElement(entity); // base converts ValueTypes and String
            return element;
        }
        #endregion XML - ENTITY mapping

        #region NextId
        /// <summary>
        ///     Incremented 'Next ID' generator.
        ///     Searches for the max existing used value
        ///     and returnes that + 1
        /// </summary>
        /// <returns></returns>
        protected override int GetNextId(Language entity = null)
        {
            var id = 0; // in case there is no element yet
            if (TheList.Any()) // element(s) exist(s) - find the largest ID
            {
                id = TheList.Max<Language, int>(e => e.ID);
            }
            return ++id;
        }
        #endregion NextId
    }
}